package com.example.adapter.liveadapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.constant.live.PartitionsBean;
import com.example.interfaces.OnItemClickListener;
import com.example.ui.R;
import com.example.ui.activitys.ViedoActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建时间 2017/9/28
 * 作者 fengxn
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 */

public class LiveTitleAdapter extends RecyclerView.Adapter<LiveTitleAdapter.LiveTitleViewHolder>
{

    public static final int ITEM_TYPE00 = 0;//放映
    public static final int ITEM_TYPE02 = 2;//御宅
    public static final int ITEM_TYPE04 = 4;//电竞
    public static final int ITEM_TYPE03 = 3;//网游
    public static final int ITEM_TYPE01 = 1;//单机
    public static final int ITEM_TYPE12 = 12;//手游
    public static final int ITEM_TYPE10 = 10;//唱舞
    public static final int ITEM_TYPE11 = 11;//手机
    public static final int ITEM_TYPE09 = 9;//绘画
    private List<PartitionsBean> data;
    private Context context;
    private String errorpng = "http://static.hdslb.com/live-static/images/mobile/android/small/xxhdpi/11.png?2016101101";

    public LiveTitleAdapter(List<PartitionsBean> data, Context context)
    {
        this.data = data;
        this.context = context;
    }

    @Override
    public LiveTitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return new LiveTitleViewHolder(LayoutInflater.from(context).inflate(R.layout.item_live_title, parent, false));
    }

    @Override
    public void onBindViewHolder(LiveTitleViewHolder holder, final int position)
    {
        holder.name.setText(data.get(position).getPartition().getName());
        holder.count.setText(String.valueOf(data.get(position).getPartition().getCount()));
        String truepng = data.get(position).getPartition().getSub_icon().getSrc();
        if (truepng.equals(""))
        {
            truepng = errorpng;
        }
        Picasso.with(context)
                .load(truepng)
                .placeholder(R.mipmap.bili_drawerbg_not_logined)
                .error(R.mipmap.bili_drawerbg_logined)
                .into(holder.iv);
        holder.rlv.setLayoutManager(new GridLayoutManager(context, 2, LinearLayout.VERTICAL, false));
        LiveAdapter adapter = new LiveAdapter(context, data.get(position).getLives());
        adapter.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(View view, int mPosition)
            {
                Intent intent = new Intent(context, ViedoActivity.class);
                intent.putExtra("data", data.get(position).getLives().get(mPosition));
                context.startActivity(intent);
            }
        });
        holder.rlv.setAdapter(adapter);

    }

    @Override
    public int getItemCount()
    {
        return data.size();
    }

    public class LiveTitleViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.item_live_title_src)
        ImageView iv;
        @BindView(R.id.item_live_tile_name)
        TextView name;
        @BindView(R.id.item_live_title_count)
        TextView count;
        @BindView(R.id.rlv_live)
        RecyclerView rlv;

        public LiveTitleViewHolder(View itemView)
        {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rlv.setItemAnimator(new DefaultItemAnimator());
//            RecyclerView.ItemDecoration decoration = new MyDecoration(this);
//            this.rlv.addItemDecoration(decoration);
        }
    }

    public class LiveViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.item_live_src)
        ImageView iv;
        @BindView(R.id.item_live_name)
        TextView name;
        @BindView(R.id.item_live_title)
        TextView title;
        @BindView(R.id.item_live_online)
        TextView online;

        public LiveViewHolder(View itemView)
        {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
